#include "cedge.h"

CEdge::CEdge()
{
    this->setFace(NULL);
    this->setHead(NULL);
    this->setTail(NULL);
    this->setTwinEdge(NULL);
    this->setNextEdge(NULL);
}

CVertex * CEdge::getHead()
{
    return this->m_pHeadVertex;
}

CVertex * CEdge::getTail()
{
    return this->m_pTailVertex;
}

CFace * CEdge::getFace()
{
    return this->m_pFace;
}

CEdge * CEdge::getTwinEdge()
{
    return this->m_pTwinEdge;
}

CEdge * CEdge::getNextEdge()
{
    return this->m_pNextEdge;
}

void CEdge::getNextPos(CPoint3f P, CPoint3f & NewP, CIntersectionPoint * &IP)
{
    IP = NULL;
    if (P == this->m_pHeadVertex->getPosition())
    {
        if (this->m_vInterPointList.size() != 0)
        {
            IP = this->m_vInterPointList.at(0);
            NewP = IP->m_Position;
        }

    }
    else
    {
        for (unsigned i = 0; i < this->m_vInterPointList.size()-1; ++i)
        {
            if (((CIntersectionPoint*)this->m_vInterPointList.at(i))->m_Position == P)
            {
                IP = this->m_vInterPointList.at(i+1);
                NewP = IP->m_Position;
            }
        }
    }

    if (!IP)
        NewP = this->m_pTailVertex->getPosition();
}

void CEdge::setHead(CVertex * pHead)
{
    this->m_pHeadVertex = pHead;
}

void CEdge::setTail(CVertex * pTail)
{
    this->m_pTailVertex = pTail;
}

void CEdge::setFace(CFace * pFace)
{
    this->m_pFace = pFace;
}

void CEdge::setTwinEdge(CEdge * pEdge)
{
    this->m_pTwinEdge = pEdge;
}

void CEdge::setNextEdge(CEdge * pEdge)
{
    this->m_pNextEdge = pEdge;
}

void CEdge::sortInterPoints()
{
    if (this->m_vInterPointList.size())
    {
        for (unsigned i = 0; i < this->m_vInterPointList.size()-1; ++i)
        {
            unsigned dist = this->getHead()->getPosition().Distance(this->m_vInterPointList.at(i)->m_Position);
            for (unsigned j = i+1; j < this->m_vInterPointList.size(); ++j)
            {
                unsigned temp = this->getHead()->getPosition().Distance(this->m_vInterPointList.at(j)->m_Position);
                if (dist > temp)
                {
                    this->switchInterPoints(i,j);
                    dist = temp;
                }
            }
        }
    }
}

void CEdge::cleanInterPoints()
{
    for (unsigned i = 0; i < this->m_vInterPointList.size()-1; ++i)
    {
        for (unsigned j = i +1; j < this->m_vInterPointList.size(); ++j)
        {
            if (this->m_vInterPointList.at(i) == this->m_vInterPointList.at(j))
            {
                this->m_vInterPointList.erase(this->m_vInterPointList.begin()+j);
                --j;
            }
        }
    }
}

void CEdge::switchInterPoints(unsigned ind1, unsigned ind2)
{
    CIntersectionPoint * temp = this->m_vInterPointList.at(ind1);
    this->m_vInterPointList.at(ind1) = this->m_vInterPointList.at(ind2);
    this->m_vInterPointList.at(ind2) = temp;
}

